package com.yneusoft.pctask.service;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yneusoft.common.model.ResponseWrapper;
import com.yneusoft.common.tool.GraphicVerification;
import com.yneusoft.common.tool.JWTUtils;
import com.yneusoft.dao.entity.Staff;
import com.yneusoft.dao.mapper.StaffMapper;
import com.yneusoft.pctask.model.GetImageCodeResult;
import com.yneusoft.pctask.model.GetNameResult;
import com.yneusoft.pctask.model.StaffLoginParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description： 登录模块 service层
 * @Author: wbf
 * @Date: 2021/11/24 9:40
 */
@Service
public class LoginManageService {
    //验证码
    @Bean
    public TimedCache getTimeCache(){
        return CacheUtil.newTimedCache(60000);
    }
    //缓存
    @Autowired
    private TimedCache<String,String> timedCache;
    //token
    private static String token = "";
    //此次登录的唯一员工
    private Staff loginStaff;
    //员工管理mapper
    @Autowired
    private StaffMapper staffMapper;
    //获得的token
    public static String getToken() {
        return token;
    }

    /**
     * 1、获取图形验证码
     * @return 图形验证码
     */
    public ResponseWrapper getImageCode(){
        //获取图形验证码
        Map<String, String> uuidAndImageCode = GraphicVerification.CreatGraphicVerification();
        GetImageCodeResult result = new GetImageCodeResult(uuidAndImageCode.get("imageCodeKey"),uuidAndImageCode.get("imageCodeBase64"));

        //将图形验证码放入入缓存中
        timedCache.put(uuidAndImageCode.get("imageCodeKey"),uuidAndImageCode.get("imageCode"));
        System.out.println("\"imageCode\":"+"\""+uuidAndImageCode.get("imageCode")+"\",");
        System.out.println("\"imageCodeKey\":" + "\""+uuidAndImageCode.get("imageCodeKey") +"\"");
        return ResponseWrapper.markSuccess(result);
    }

    /**
     * 2、员工登录
     * @return token
     */
    public ResponseWrapper staffLogin(StaffLoginParam param){
        //自定义响应信息
        String[] msg = {
                "该员工已离职！",
                "用户名或密码错误！",
                "验证码错误！",
                "操作异常，请刷新验证码！"
        };
        //通过手机号和密码登录
        LambdaUpdateWrapper<Staff> checkLogin = new LambdaUpdateWrapper<>();
        checkLogin
                .eq(Staff::getPhone,param.getPhone())
                .eq(Staff::getPassword,param.getPassword())
        ;
        loginStaff = staffMapper.selectOne(checkLogin);

        /**
         * 登录校验
         */
        if(loginStaff == null){
            return ResponseWrapper.markSuccess(msg[1]);
        }
        else if(loginStaff.getStateId() == 0){
            return ResponseWrapper.markSuccess(msg[0]);
        }
        else if(timedCache.get(param.getImageCodeKey()) != null){
            if (!timedCache.get(param.getImageCodeKey()).equals(param.getImageCode())){
                return ResponseWrapper.markSuccess(msg[2]);
            }
        }
        else {return ResponseWrapper.markSuccess(msg[3]);}

        /**
         * 生成token
         */
        Map<String,String> map = new HashMap<>();
        map.put("aid",loginStaff.getAid().toString());
        token = JWTUtils.getToken(map);
        map.remove("aid");
        map.put("token",token);
        //返回token
        return ResponseWrapper.markSuccess(map);
    }

    /**
     * 3、获取所登录员工的名称
     * @return 登录员工的名称
     */
    public ResponseWrapper getStaffName(){
        GetNameResult result = new GetNameResult();
        //获取登录员工的姓名
        String name = loginStaff.getName();
        result.setName(name);
        return ResponseWrapper.markSuccess(result);
    }
}
